<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <link rel="stylesheet" href="../../lay/layui/css/layui.css" media="all">
  <link rel="stylesheet" href="../../lay/style/admin.css" media="all">
  <link rel="stylesheet" href="../../lay/style/yogee.css" media="all">
  <link rel="stylesheet" href="../../lay/lib/extend/dtree/dtree.css" media="all">
  <link rel="stylesheet" href="../../lay/lib/extend/dtree/font/dtreefont.css" media="all">
  <style>

    .layui-card-header .layui-icon {
      position: initial;
      margin-top: 0;
      line-height: unset;
    }

    .layui-upload-img {
      width: 200px;
      /*height: 90px;*/
    }
  </style>
</head>
<body>
<div class="layui-fluid">
  <div class="layui-card">
    <div class="layui-fluid">
      <div class="layui-tab-content">
        <div class="layui-form" lay-filter="layForm">
          <div class="layui-form-item">
            <label for="productId" class="layui-form-label">入库商品：</label>
            <div class="layui-input-inline">
              <select name="productId" id="productId">
                <option value="">请选择入库商品</option>
              </select>
            </div>
          </div>
          <div class="layui-form-item">
            <label for="price" class="layui-form-label">单价：</label>
            <div class="layui-input-inline">
              <input class="layui-input" id="price" name="price" placeholder="">
            </div>
          </div>
          <div class="layui-form-item">
            <label for="counts" class="layui-form-label">数量：</label>
            <div class="layui-input-inline">
              <input class="layui-input" id="counts" name="counts" placeholder="">
            </div>
          </div>
          <div class="layui-form-item">
            <label for="buyAt" class="layui-form-label">购置时间：</label>
            <div class="layui-input-inline">
              <input class="layui-input" id="buyAt" name="buyAt" placeholder="">
            </div>
          </div>
          <div class="layui-form-item">
            <label for="total" class="layui-form-label">总金额：</label>
            <div class="layui-input-inline">
              <input class="layui-input" id="total" name="total" placeholder="">
            </div>
          </div>
          <div class="box-footer layui-text-center">
            <div class="align-center">
              <button lay-submit class="submit layui-btn layui-btn-normal" lay-filter="*"
                      data-loading="<i class='layui-icon layui-icon-loading layui-icon layui-anim layui-anim-rotate layui-anim-loop'></i> 提交中">
                提交
              </button>
              <a class="layui-btn layui-btn-warm" href="javascript:history.back(-1)">取消</a>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

<script src="../../util/httpUtil.js"></script>
<script src="../../lay/layui/layui.js"></script>
<script>
  layui.use(['form', 'jquery', 'layer', 'element', 'laydate'], function () {
    var $ = layui.$;
    var form = layui.form;
    var inputId = getUrlParam('id') ? getUrlParam('id') : "";
    var stockId = getUrlParam('stockId') ? getUrlParam('stockId') : "";

    var laydate = layui.laydate;

    laydate.render({
      elem: '#buyAt' //指定元素
    });

    //数据回显
    if (inputId !== "") {
      HTTPUtil.post('/oa/input/get', {id: inputId})
          .then(function (json) {
            if (json.state === 'success') {
              form.val("layForm", {
                "productId": json.data.productId,
                "counts": json.data.counts,
                "buyAt": json.data.buyAt,
                "price": json.data.price,
                "total": json.data.total
              });
              stockId = json.data.stockId;
              getProductList(json.data.productId);
            } else {
              layer.msg(json.msg, {icon: 2});
            }
          });
    } else {
      getProductList("");
    }

    //异步请求分类
    function getProductList(productId) {
      HTTPUtil.post('/oa/product/all', {})
          .then(function (json) {
            if (json.state === 'success') {
              for (var i = 0; i < json.data.length; i++) {
                if (json.data[i].id === productId) {
                  $('#productId').append('<option value="' + json.data[i].id + '" selected>' + json.data[i].productName + '</option>')
                } else {
                  $('#productId').append('<option value="' + json.data[i].id + '">' + json.data[i].productName + '</option>')
                }
              }
              form.render('select');
            } else {
              layer.msg(json.msg, {icon: 2});
            }
          })
    }

    form.on('submit(*)', function (data) {
      $(this).addClass('layui-btn-disabled');
      $(this).html($(this).data('loading'));
      var productId = $('#productId').val();
      var counts = $('#counts').val();
      var buyAt = $('#buyAt').val();
      var price = $('#price').val();
      var total = $('#total').val();
      HTTPUtil.post('/oa/input/store',
              {
                id: inputId,
                stockId: stockId,
                productId: productId,
                counts: counts,
                price: price,
                buyAt: buyAt,
                total: total
            }).then(function (json) {
            if (json.state === 'success') {
              layer.msg(json.msg,{icon: 1,time:500},function () {
                window.location.href = 'inputList.html';
              });
            } else {
              layer.msg(json.msg,{icon: 2});
            }
          })

    });

  });

</script>
</body>
</html>